/**
 * @file ipeParam.h
 * @author SDK_TEAM
 * @brief
 * @version 0.1
 * @date 2022-11-09
 *
 * Copyright:
 * © 2018 北京灵汐科技有限公司 版权所有。
 * 注意：以下内容均为北京灵汐科技有限公司原创，未经本公司允许，不得转载，否则将视为侵权；对于不遵守此声明或者其他违法使用以下内容者，本公司依法保留追究权。
 * © 2018 Lynxi Technologies Co., Ltd. All rights reserved.
 * NOTICE: All information contained here is, and remains the property of Lynxi.
 * This file can not be copied or distributed without the permission of Lynxi
 * Technologies Co., Ltd.
 *
 */

#pragma once

#include <lyn_api.h>
#include <iostream>
#include "util.hpp"

struct ImageMergeInfo {
  lynFrame_t *frame;
  lynVdecOutInfo_t videoInfo;
  int32_t x;
  int32_t y;
  int32_t width;
  int32_t heigh;
};

class IpeParamBase {
 protected:
  // 模型宽高
  int m_iModelWidth = 0;
  int m_iModelHeight = 0;

  // 输入图片信息
  int m_iImgWidth = 0;
  int m_iImgHeight = 0;
  lynPixelFormat_t m_format = LYN_PIX_FMT_NONE;

 public:
  // ipe输入信息
  lynIpePicDesc_t m_oPicDescIn;
  // ipe输出信息
  lynIpePicDesc_t m_oPicDescOut;
  // ipe配置信息
  lynIpeConfigDesc_t m_oConfigDesc;

 public:
  IpeParamBase(int iModelWidth, int iModelHeight)
      : m_iModelWidth(iModelWidth), m_iModelHeight(iModelHeight) {
    // 创建ipe相关结构体
    CHECK_ERR(lynIpeCreatePicDesc(&m_oPicDescIn));
    CHECK_ERR(lynIpeCreatePicDesc(&m_oPicDescOut));
    CHECK_ERR(lynIpeCreateConfigDesc(&m_oConfigDesc));
  }

  virtual ~IpeParamBase() {
    // 释放ipe相关结构体
    CHECK_ERR(lynIpeDestroyPicDesc(m_oPicDescIn));
    CHECK_ERR(lynIpeDestroyPicDesc(m_oPicDescOut));
    CHECK_ERR(lynIpeDestroyConfigDesc(m_oConfigDesc));
  }

  // 设置输入图片的信息（接口分开为了同一类型的图片该方法可只调一次）
  virtual void SetImgInfo(int32_t width, int32_t height,
                          lynPixelFormat_t format) {
    m_iImgWidth = width;
    m_iImgHeight = height;
    m_format = format;
  }

  void MergeImage(lynStream_t stream, void *outputData,
                  std::vector<ImageMergeInfo> &inputDatas) {
    CHECK_ERR(lynIpeResetPicDesc(m_oPicDescOut));
    CHECK_ERR(lynIpeResetConfigDesc(m_oConfigDesc));
    CHECK_ERR(lynIpeSetOutputPicDesc(m_oPicDescOut, outputData, m_iImgWidth,
                                     m_iImgHeight, m_format));
    for (auto inputData : inputDatas) {
      if (inputData.frame == nullptr) {
        continue;
      }
      CHECK_ERR(lynIpeResetPicDesc(m_oPicDescIn));
      CHECK_ERR(lynIpeSetInputPicDesc(m_oPicDescIn, inputData.frame->data,
                                      inputData.videoInfo.width,
                                      inputData.videoInfo.height, m_format));
      CHECK_ERR(lynIpeSetCropConfig(m_oConfigDesc, 0, 0,
                                    inputData.videoInfo.width,
                                    inputData.videoInfo.height));
      CHECK_ERR(lynIpeSetOutputRect(m_oConfigDesc, inputData.x, inputData.y,
                                    inputData.width, inputData.heigh, false));
      CHECK_ERR(lynIpeProcessAsync(stream, m_oPicDescIn, m_oPicDescOut,
                                   m_oConfigDesc));
    }
  }

  // IPE处理虚函数
  virtual void CalcParam(lynStream_t stream, void *inputData,
                         void *outputData){};
};
